package com.fjnu.controller;

import com.fjnu.domain.entity.Tag;
import com.fjnu.domain.vo.ResponseResult;
import com.fjnu.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/blog-api/v0.1/tag")
public class TagController {

    @Autowired
    private TagService tagService;

    @GetMapping("/list")
    public ResponseResult<List<Tag>> list() {
        List<Tag> tags = tagService.getAllTags();

        if (tags == null) {
            throw new RuntimeException("标签服务返回空结果");
        }

        if (tags.isEmpty()) {
            return ResponseResult.success("查询成功，但未找到标签数据", Collections.emptyList());
        }

        return ResponseResult.success(tags);
    }

    @PostMapping("/add")
    public ResponseResult<String> add(@RequestBody Tag tag) {
        int result = tagService.addTag(tag);
        if (result > 0) {
            return ResponseResult.success("标签添加成功");
        }
        return ResponseResult.error("标签添加失败");
    }

    @GetMapping("/delete")
    public ResponseResult<String> delete(@RequestParam String tagId) {
        int result = tagService.deleteTag(Integer.parseInt(tagId));
        if (result > 0) {
            return ResponseResult.success("标签删除成功");
        }
        return ResponseResult.error("标签删除失败");
    }
}